package client.comm.baoding.fragment

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import client.comm.commlib.base.BaseFragment
import client.comm.commlib.dialog.CameraOrSelectDialog
import client.comm.commlib.toast.toast
import client.comm.commlib.widget.EmptyLayout
import client.comm.baoding.R
import client.comm.baoding.adapter.AddrMangerAdapter
import client.comm.baoding.databinding.FragmentAddroneBinding
import client.comm.baoding.databinding.FragmentSetxxoneBinding
import client.comm.baoding.dialog.QrDelAddrDialog
import client.comm.baoding.ui.AddAddrActivity
import client.comm.baoding.ui.SetSkxxActivity
import client.comm.baoding.ui.vm.AddrMangerViewModel
import client.comm.baoding.ui.vm.SetskxxViewModel
import kotlinx.coroutines.flow.*

class AddrOneFragment : BaseFragment<FragmentAddroneBinding, AddrMangerViewModel>() {
    override fun getViewId(): Int {
        return R.layout.fragment_addrone
    }

    override fun initViewModel(): AddrMangerViewModel = createViewModel()
    val adapter: AddrMangerAdapter by lazy {
        AddrMangerAdapter(requireContext()).apply {
//            isSelectMode = requireActivity().intent.getBooleanExtra("select", false)
            del = {
                QrDelAddrDialog(requireActivity()).apply {
                    qrClick = {
                        viewModel.del(it)
                    }
                }.show()
            }
            edit = {
                startActivity(
                    Intent(
                        requireContext(),
                        AddAddrActivity::class.java
                    ).apply {
                        putExtra("bean", it)
                        putExtra("isEdit", true)
                    })
            }
            itemClick = {
                if (requireActivity().intent.getBooleanExtra("select", false)) {
                    requireActivity().setResult(Activity.RESULT_OK, Intent().apply {
                        putExtra("address", it)
                    })
                    requireActivity().finish()
                }
            }
            defItem = {

                viewModel.defAddr(it)
            }
        }
    }

    override fun initWidget() {
        binding.event = this
        binding.vm = viewModel

        binding.recyclerView.layoutManager = LinearLayoutManager(requireContext())
        binding.recyclerView.adapter = adapter
        binding.emptyLayout.setOnClickListener { viewModel.call() }

        viewModel.call.observe(requireActivity(), Observer {
            it?.apply {
                if (isSuccess) {
                    data?.let {
                        adapter.submitList(it.address_list)
                        if (it.address_list != null && it.address_list.size > 0) {
                            binding. emptyLayout.visibility = View.GONE
                        } else {
                            binding.emptyLayout.errorType = (EmptyLayout.NODATA)
                        }
                    }
                } else {
                    if (adapter.itemCount == 0)
                        binding.emptyLayout.errorType = (EmptyLayout.NETWORK_ERROR)
                }
            }
        })
        viewModel.delRet.observe(requireActivity(), Observer {
            it?.apply {
                if (isSuccess) {
                    toast("删除成功")
                    viewModel.call()
                }
            }
        })
        viewModel.defRet.observe(requireActivity(), Observer {
            it?.apply {
                if (isSuccess) {
                    toast("设置成功")
                    viewModel.call()
                }
            }
        })

    }

    override fun onResume() {
        super.onResume()
        viewModel.call()
    }

    fun addAddr() {
        startActivity(Intent(requireContext(), AddAddrActivity::class.java))
    }
}